﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using FluentValidation.Results;
using Microhard.WarehouseInsp.Buz.BuzEnum;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class SupervisionDetailModelValidator : AbstractValidator<SupervisionDetailModel>
    {
        public SupervisionDetailModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.WarnAmount).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedWarnAmount).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Guardline).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedGuardline).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Worth).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedWorth).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    if (u.WarnAmount > u.Guardline)
                    {
                        return new ValidationFailure("WarnAmount", SupervisionDetailMsg.WarmGThanGuard);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.WarnAmount).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedWarnAmount).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Guardline).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedGuardline).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Worth).GreaterThan(0).WithMessage(SupervisionDetailMsg.NeedWorth).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    if (u.WarnAmount > u.Guardline)
                    {
                        return new ValidationFailure("WarnAmount", SupervisionDetailMsg.WarmGThanGuard);
                    }
                    //如果是保存或者退回状态，才能修改数据
                    if (u.Status != (int)PrjStatus.RollBack || u.Status != (int)PrjStatus.Saved)
                    {
                        return new ValidationFailure("CannotUpdate", "CannotUpdate");
                    }
                    return null;
                });
            });
        }
    }
}
